<?php

header('Content-Type: text/html; charset=utf-8');
include_once('facilitador.php');
conexao();
bloqueia();

//$ret = "produto-listar";
$ret = "produto-cadastro-cores&id=";
$tab = "produto";

$categoria = @$_POST['categoria'];
$subcategoria = @$_POST['subcategoria'];
$titulo = strip($_POST['titulo']);
$resumo = strip($_POST['resumo']);
$descricao = add_strip($_POST['descricao']);
$like = strip($_POST['like']);
$indicacao = @$_POST['indicacao'];
$valor = strip($_POST['valor']);
//Remove o R$ do valor
if(isset($valor)){
$valor = str_replace('R$', '', $valor);
//Converte o valor para salvar no banco
$valor = moeda($valor);
}

if ($_GET['funcao'] == "Inserir") {
    $sql = $MQ("INSERT INTO " . $tab . " VALUES ('', '$categoria', '$subcategoria', '$titulo', '$resumo', '$descricao', '$like','$indicacao', '$valor' );");
    $ultimo_id = mysql_insert_id();
    echo "<script type='text/javascript'>alert('Cadastro realizado'); window.location = 'index.php?pagina=".$ret.$ultimo_id."'; </script>";
    exit();
} 

/* ############################################################ */

if ($_GET['funcao'] == "Editar") {
    $id = $_GET['id'];
    $sql = $MQ("UPDATE " . $tab . " SET id_cat =  '$categoria', id_sub = '$subcategoria', titulo = '$titulo', resumo = '$resumo', 
           descricao = '$descricao', `like` = '$like', indicacao = '$indicacao', preco = '$valor' WHERE id = '$id'");
    echo "<script type='text/javascript'>alert('Alteração realizada'); window.location = 'index.php?pagina=".$ret.$id."  ';</script>";
    exit();
}

/* ######################################################### */

if ($_GET['funcao'] == "Excluir") {
    $id = $_GET['id'];
    $sql = $MQ("DELETE FROM " . $tab . " WHERE id = '$id'");
    echo "<script type='text/javascript'>alert('Cadastro Excluído.'); window.location = 'index.php?pagina=produto-listar' ;</script>";
}
/* ############################################################ */
?>